package system;
import java.sql.*;
public class getQuestions{
	int count;
	
	public int getCount(String testid){
		int no=0;
		Connection con;
	    Statement sql;
	    ResultSet rs;
	    try{
	    	Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
	    	}
	    catch(Exception e){}
   
        try{
		String uri = "jdbc:sqlserver://127.0.0.1:1433;DatabaseName=test_system";
		String user = "sa";
		String password = "sa";
		con=DriverManager.getConnection(uri,user,password);
		sql=con.createStatement();
	    rs=sql.executeQuery("select COUNT from testing where test_id='"+testid+"'");
	    rs.next();
	    no=rs.getInt(1);
		con.close();
	    } catch(SQLException e1){}
	    return no;
	}
	
	public String[][] getQuestionsList(String testid){
		count=getCount(testid);
		String[][] questionsList=new String[count][3];
		
		int index=0;
		Connection con;
	    Statement sql;
	    ResultSet rs;
	    try{
	    	Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
	    	}
	    catch(Exception e){}
   
        try{
		String uri = "jdbc:sqlserver://127.0.0.1:1433;DatabaseName=test_system";
		String user = "sa";
		String password = "sa";
		con=DriverManager.getConnection(uri,user,password);
		sql=con.createStatement();
		rs=sql.executeQuery("SELECT * from questions where test_id='"+testid+"'");
		while(rs.next()){
			questionsList[index][0]=rs.getString(1);
			questionsList[index][1]=rs.getString(3);
			questionsList[index][2]=rs.getString(4);	
			index+=1;		
		}
		con.close();
	    } catch(SQLException e1){}
	    return questionsList;
	}
	
	public String[] getSingle(String questionid){
		String[] questionInf=new String[6];
		Connection con;
	    Statement sql;
	    ResultSet rs;
	    try{
	    	Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
	    	}
	    catch(Exception e){}
   
        try{
		String uri = "jdbc:sqlserver://127.0.0.1:1433;DatabaseName=test_system";
		String user = "sa";
		String password = "sa";
		con=DriverManager.getConnection(uri,user,password);
		sql=con.createStatement();
		rs=sql.executeQuery("SELECT * from single_selection where question_id='"+questionid+"'");
		if(rs.next()){
			for(int i=0;i<6;i++){
				questionInf[i]=rs.getString(i+2);
				}		
		}
		con.close();
	    } catch(SQLException e1){}
	    return questionInf;
	}
	
	public String[] getMultiple(String questionid){
		String[] questionInf=new String[9];
		
		Connection con;
	    Statement sql;
	    ResultSet rs;
	    try{
	    	Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
	    	}
	    catch(Exception e){}
   
        try{
		String uri = "jdbc:sqlserver://127.0.0.1:1433;DatabaseName=test_system";
		String user = "sa";
		String password = "sa";
		con=DriverManager.getConnection(uri,user,password);
		sql=con.createStatement();
		rs=sql.executeQuery("SELECT * from multiple_selection where question_id='"+questionid+"'");
		if(rs.next()){
			for(int i=0;i<9;i++){
				questionInf[i]=rs.getString(i+2);
				}		
		}
		con.close();
	    } catch(SQLException e1){}
	    return questionInf;
	}
	
	public String[] getSubject(String questionid){
		String[] topic=new String[2];
		
		Connection con;
	    Statement sql;
	    ResultSet rs;
	    try{
	    	Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
	    	}
	    catch(Exception e){}
   
        try{
		String uri = "jdbc:sqlserver://127.0.0.1:1433;DatabaseName=test_system";
		String user = "sa";
		String password = "sa";
		con=DriverManager.getConnection(uri,user,password);
		sql=con.createStatement();
		rs=sql.executeQuery("SELECT * from subjective_topic where question_id='"+questionid+"'");
		if(rs.next()){
			topic[0]=rs.getString(2);
			topic[1]=rs.getString(3);
				
		}
		con.close();
	    } catch(SQLException e1){}
	    return topic;
	}
	
	
}